package org.bigjava.biz.impl;

import org.bigjava.bean.User;
import org.bigjava.biz.UserService;
import org.bigjava.common.Cryptograghy;
import org.bigjava.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Qualifier("userMapper")
    @Autowired
    UserMapper userMapper;

    //业主账号校验
    public User userJiaoYan(String username){ return userMapper.userJiaoYan(username); }

    //业主账号和业主姓名模糊查询
    public List<User> userMoHu(String username,String realname){ return userMapper.userMoHu(username,realname); }

    //修改业主信息
    public int userXiuGai(User user){
        User usera = userId(user.getId());
        if(user.getPassword()==""){
            user.setPassword(usera.getPassword());
        }else{
            //sha1加密
            try {
                user.setPassword(Cryptograghy.sha1(user.getPassword()));
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return userMapper.userXiuGai(user);
    }
    //查业主信息
    public User userId(int id){ return userMapper.userId(id); }
    //业主假删除
    public void userShanChu(int id){ userMapper.userShanChu(id); }
    //业主总人数
    public int userZong(){
        return userMapper.userZong();
    }
    //业主分页查询
    public List<User> userFenYe(int offset,int length){
        return userMapper.userFenYe(offset,length);
    }
    //业主账号密码登录
    public User userDengLu(String username,String password){
        try {
            password = Cryptograghy.sha1(password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return userMapper.userDengLu(username,password);
    }

    public List<User> userQuan(){
        return userMapper.userQuan();
    }

    //业主添加
    public int userAdd(User user){
        Date time = new Date();
        user.setRegisteDate(time);
        //sha1加密
        try {
            user.setPassword(Cryptograghy.sha1(user.getPassword()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return userMapper.userAdd(user);
    }
}
